<template>
    <div>
        <base-list ref="specialTransportLineMachine" :condition="condition" :pageSearch="pageSearch">
            <el-form :inline="true" :model="condition" class="search-form" slot="search" label-width="80px">
                <el-row>
                    <el-col :span="7" class="search-width4">
                        <el-form-item label="专 运 线">
                            <!-- <el-input v-model="condition['search_LIKE_lineNo']"></el-input> -->
                            <auto-search-line v-model="condition['search_LIKE_resourceNo']"></auto-search-line>
                            <!--<auto-search-resource searchType="JOB_RY" searchName="resourceCode" @selectName="v=>condition['search_LIKE_resourceCode']=v"></auto-search-resource>-->
                        </el-form-item>
                    </el-col>
                    <el-col :span="2">
                        <el-button type="primary" icon="el-icon-search" @click="loadData" class="ml-80">查 询</el-button>
                    </el-col>
                </el-row>
            </el-form>
            <el-row slot="status">
                <el-col :span="24">
                    <el-button type="primary" formVisible="true" icon="el-icon-plus" @click="handleDialogFormOpen(false)" v-if="power('WJOB_BASEMGR_TRANSPORTLINEMACHINE:ADD')">
                            新增
                        </el-button>
                </el-col>
            </el-row>
            <el-table slot="list" :data="specialTransportLineMachine" border stripe max-height="550">
                <el-table-column prop="lineNo" label="专运线"  min-width="100"></el-table-column>
                <el-table-column prop="orgName" label="所属单位"  min-width="200"></el-table-column>
                <el-table-column prop="deptName" label="所属部门"  min-width="200"></el-table-column>
                <el-table-column v-for="(item,index) in tableHeader" :prop="item.prop" :label="item.label" :key="index"  min-width="120">
                </el-table-column>
                <el-table-column fixed="right" align="center" label="操作"  width="140">
                    <template slot-scope="scope">
                        <!-- v-if="power('WJOB_BASEMGR_TRANSPORTLINEMACHINE:UPD')" -->
                        <el-button size="small" type="text" @click="handleDialogFormOpen(true,scope.$index)">
                            编辑
                        </el-button>
                        <!-- v-if="power('WJOB_BASEMGR_TRANSPORTLINEMACHINE:DEL')" -->
                        <el-button type="text" class="danger" size="small" @click="handleRemove(scope.row)">删除
                        </el-button>
                    </template>
                </el-table-column>
            </el-table>
        </base-list>
        <special-transport-line-machine-form ref="specialTransportLineMachineForm" @load="loadData"></special-transport-line-machine-form>
    </div>
</template>

<script>
    import { deleteSpecialTransportLineMachine, getSpecialTransportLineMachineList } from "@/api/baseResource/baseResource"
    import autoSearchOrg from '@/components/autocomplete/autoSearchOrg'
    import autoSearchResource from '@/components/autocomplete/autoSearchResource'
    import autoSearchLine from '@/components/autocomplete/autoSearchLine'
    import specialTransportLineMachineForm from './specialTransportLineMachineForm'

    export default {
        name: "specialTransportLineMachine",
        components: {
            specialTransportLineMachineForm,
            autoSearchOrg,
            autoSearchResource,
            autoSearchLine
        },
        data() {
            return {
                condition: {}, // 查询条件
                specialTransportLineMachine: [{
                    specialTransportLineMachine:[{machineName:''},{machineName:''},{machineName:''},]
                }], // table数组
                machine:[],
                tableHeader:[],
                pageSearch: getSpecialTransportLineMachineList, // 分页查询api接口
                id: ''
            }
        },
        mounted() {
            this.$nextTick(() => {
                this.loadData()
            })
        },
        activated () {
            this.loadData()
        },
        methods: {
            loadData() {
                this.$refs.specialTransportLineMachine.loadData().then(res=>{
                    this.machine = JSON.parse(JSON.stringify(res.aaData))
                    let data = JSON.parse(JSON.stringify(res.aaData))
                    let arr = []
                    for (let i = 0; i < data.length; i++) {
                        let el = data[i];
                        for (var tmp in el) {
                            if (tmp === 'specialTransportLineMachine' &&el[tmp]&& el[tmp].length > 0) {
                                for (let j = 0; j < el[tmp].length; j++) {
                                    let et = el[tmp][j];
                                    for (var t in et) {
                                        if(t==='machineName'){
                                            let key = t + [j+1]
                                            el[key] = et[t]
                                        }
                                    }
                                }
                            }
                        }
                    }
                    let sort = []
                    for (let i = 0; i < data.length; i++) {
                        delete data[i].specialTransportLineMachine
                        sort.push(data[i])
                    }
                    let num = sort.sort((a, b) => Object.keys(b).length - Object.keys(a).length)[0]
                    for (var tmp in num) {
                        let label = ''
                        if (tmp.indexOf('machineName') > -1) {
                            label = '机械' + tmp.split('machineName')[1]
                            arr.push({
                                prop: tmp,
                                label: label
                            })
                        }
                    }
                    this.tableHeader=arr
                    // console.log(arr)
                    this.specialTransportLineMachine = data
                    // console.log(this.specialTransportLineMachine);
                    // console.log(data);
                })
            },
            handleDialogFormOpen(bool, index) {
                if (bool) {
                    let form = JSON.parse(JSON.stringify(this.machine[index]))
                    let special=[{
                        machineName:'',
                        machineId:'',
                        startParkingLot:'',
                        endParkingLot:'',
                        valid:false,
                        remark:'',
                    }]
                    this.$refs.specialTransportLineMachineForm.form = {
                        id:form.lineId,
                        lineNo: form.lineNo,
                        maxParkingNum: form.maxParkingNum,
                        sidehilRoadDire: form.sidehilRoadDire,
                        riverSideRoadDire: form.riverSideRoadDire,
                        orgId: this.$store.state.user.currentUser.org && this.$store.state.user.currentUser.org.id,
                        orgName: this.$store.state.user.currentUser.org && this.$store.state.user.currentUser.org.name,
                        deptId: form.deptId,
                        deptName: form.deptName,
                        specialTransportLineMachine: form.specialTransportLineMachine||special,
                        valid: form.valid,
                    }
                    this.$refs.specialTransportLineMachineForm.isEdit = true
                    this.$refs.specialTransportLineMachineForm.maxLot = form.maxParkingNum
                    this.$refs.specialTransportLineMachineForm.dialogForm = true
                } else {
                    this.$refs.specialTransportLineMachineForm.dialogForm = true
                }
            },
            handleRemove(item) {
                this.$confirm(`是否删除该数据?`, '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    deleteSpecialTransportLineMachine(item.lineId).then(res => {
                        this.$message({
                            type: 'success',
                            message: `删除成功!`
                        });
                        this.loadData()
                    })
                }).catch(() => {
                    this.$message({
                        type: 'info',
                        message: '已取消'
                    });
                });
            }
        },
    }

</script>

<style scoped>


</style>
